package com.soar.recruitment.common.ui.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;

import com.soar.recruitment.common.app.SOARBaseApplication;
import com.soar.recruitment.common.bll.ActivityManager;
import com.soar.recruitment.common.ui.interfaces.OnLoadClickListener;
import com.soar.recruitment.common.ui.view.LoadFrameLayout;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.SupportActivity;

/**
 * Created by Wen
 * Email: wenpeng@qianshengqian.com
 * Date: 2016/12/20
 * Description：全屏的 - 带Loadding的处理基类
 */
public abstract class SOARMainBaseActivity extends SupportActivity {

    protected Activity mActivity;
    protected FragmentManager mFragmentManager;
    protected Bundle mSavedInstanceState;

    protected View mRootView;

    protected LoadFrameLayout mLoadFrameLayout;

    private Unbinder unbinder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //getWindow().getDecorView().setBackgroundResource(0);
        mSavedInstanceState = savedInstanceState;
        mActivity = this;
        mFragmentManager = getSupportFragmentManager();
        mRootView = LayoutInflater.from(this).inflate(initLayout(), null);
        setContentView(mRootView);
        initLoadingView();
        init(savedInstanceState);
    }

    /**
     * 初始化
     */
    private void init(@Nullable Bundle savedInstanceState) {
        bindIOC();
        registEventBus();
        initVariable();
        initView(savedInstanceState);
        initData();
    }

    /**
     * Init Loading
     */
    private void initLoadingView() {
        if (isShowLoading()) {
//            mLoadFrameLayout = ButterKnife.findById(mRootView, R.id.load_root_view);
            mLoadFrameLayout.showLoadingView();
            mLoadFrameLayout.setOnLoadClickListener(new OnLoadClickListener() {
                @Override
                public void retryLoad() {
                    mLoadFrameLayout.showLoadingView();
                    onRetryLoading();
                }
            });
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        ActivityManager.registShowing();
    }

    @Override
    protected void onPause() {
        super.onPause();
        ActivityManager.unRegistShowing();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unBindIOC();
        unRegistEventBus();
    }

    @Override
    public void finish() {
        super.finish();
    }

    private void bindIOC() {
        unbinder = ButterKnife.bind(this);
    }

    private void unBindIOC() {
        unbinder.unbind();
    }

    private void registEventBus() {
        if(useEventBus()) {
            EventBus.getDefault().register(this);
        }
    }

    private void unRegistEventBus() {
        if(useEventBus()) {
            if(EventBus.getDefault().isRegistered(this)){
                EventBus.getDefault().unregister(this);
            }
        }
    }

    protected String getResStr(int strId) {
        return SOARBaseApplication.getInstance().getResources().getString(strId);
    }

    protected void onRetryLoading() {

    }

    /**
     * 是否使用 EventBus - 默认不处理
     * @return
     */
    protected boolean useEventBus() {
        return false;
    }


    /**
     * 是否显示 Loading - 默认不现实
     * @return
     */
    protected boolean isShowLoading() {
        return false;
    }

    /**
     * 淡入淡出
     * @return
     */
    protected boolean isAlpshAnimation() {
        return false;
    }

    public abstract int initLayout();
    public abstract void initVariable();
    public abstract void initView(@Nullable Bundle savedInstanceState);
    public abstract void initData();

}
